package day21;
//冒泡排序

public class BubbleSort {
    public BubbleSort(int[] arr) {
        for (int j = 0; j < arr.length - 1; j++) {
            for (int i = 0; i < arr.length - (j + 1); i++) {
                if (arr[i] > arr[i + 1]) {   //该段逻辑为冒泡排序核心
                    int temp = arr[i];       //
                    arr[i] = arr[i + 1];     //
                    arr[i + 1] = temp;       //
                }
            }

            System.out.println("遍历数组");
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
                if (i == arr.length - 1) {
                    System.out.println("");
                }
            }

            if (j == arr.length - 2) {
                System.out.print("排序结果为：");
                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[i] + " ");
                }
            }
        }
    }
}